package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.HospitalStatusBean;

import java.util.List;

/**
 * Created by ray_lei on 2016/7/6.
 */
public class AllApater extends BaseAdapter {

    private List<HospitalStatusBean> kinds;

    private LayoutInflater mInflater;//得到一个LayoutInfalter对象用来导入布局

    private DisplayListener listener;

    private Context context;


    /**构造函数*/
    public AllApater(Context context, List<HospitalStatusBean> kinds) {
        this.mInflater = LayoutInflater.from(context);
        this.kinds = kinds;
        this.context = context;
    }

    @Override
    public int getCount() {
        return kinds.size();//返回数组的长度
    }

    @Override
    public Object getItem(int position) {
        return kinds.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**书中详细解释该方法*/
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        //观察convertView随ListView滚动情况
        Log.v("MyListViewBase", "getView " + position + " " + convertView);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.kind_item_list,null);
            holder = new ViewHolder();
            /**得到各个控件的对象*/
            holder.layout = (LinearLayout) convertView.findViewById(R.id.kind_layout);
            holder.name = (TextView) convertView.findViewById(R.id.kind_tv_name);
            holder.ischoose = (Button) convertView.findViewById(R.id.kind_btn);
            convertView.setTag(holder);//绑定ViewHolder对象
        }
        else{
            holder = (ViewHolder)convertView.getTag();//取出ViewHolder对象
        }
        /**设置TextView显示的内容，即我们存放在动态数组中的数据*/
        holder.name.setText(kinds.get(position).getName());
        holder.ischoose.setVisibility(View.INVISIBLE);
        if(kinds.get(position).isSelect()){
            holder.name.setTextColor(context.getResources().getColor(R.color.light_blue));
            holder.ischoose.setVisibility(View.VISIBLE);
        }else{
            holder.name.setTextColor(context.getResources().getColor(R.color.grey_text1));
            holder.ischoose.setVisibility(View.INVISIBLE);
        }
//        holder.layout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                holder.name.setTextColor(context.getResources().getColor(R.color.light_blue));
//                holder.ischoose.setVisibility(View.VISIBLE);
//                if(kinds!=null){
//                    for(int i = 0;i < kinds.size();i++){
//                        if(i==position){
//                            kinds.get(position).setIsSelect(true);
//                        }else{
//                            kinds.get(position).setIsSelect(false);
//                        }
//                    }
//                }
//                listener.onDisplay(position);
//            }
//        });




        return convertView;
    }



/**存放控件*/
public final class ViewHolder{
    public LinearLayout layout;
    public TextView name;
    public Button ischoose;
        }

    public interface DisplayListener {
        void onDisplay(int position);
    }

    public void setDisplayListener(DisplayListener listener){
        this.listener = listener;
    }
}
